nums = [1, 2, 3, 1], k = 3

var containsNearbyDuplicate = function (nums, k) {
    let map = {};
    for (let i = 0; i < nums.length; i++) {
        const num = nums[i];
        if (num in map && i - map[num] <= k) {
            return true;
        }
        map[num] = i; // 每次更新当前元素的索引
    }
    return false;
};

console.log(containsNearbyDuplicate(nums, k));
